package impl

import (
	"gitee.com/go-course/go12/devcloud-mini/cmdb/apps/resource"
	"github.com/go-playground/validator/v10"
	"github.com/infraboard/mcube/ioc"
	"github.com/infraboard/mcube/ioc/config/logger"
	ioc_mongo "github.com/infraboard/mcube/ioc/config/mongo"
	"github.com/rs/zerolog"
	"go.mongodb.org/mongo-driver/mongo"
	"google.golang.org/grpc"
)

func init() {
	ioc.Controller().Registry(&impl{})
}

// resource 控制器
type impl struct {
	// ioc 托管继承
	ioc.ObjectImpl
	// 实现了对外的rpc
	resource.UnimplementedRPCServer

	// 需要存入的数据库
	col *mongo.Collection
	// 模块的日志
	log *zerolog.Logger
	// 模块校验器
	validator *validator.Validate

	// [resoruce]
	// tenxen_secret = "a"
	TencentSecret string `toml:"tencent_secret"`
}

func (i *impl) Init() error {
	// 需要依赖 mongodb, 通过Ioc的依赖注入 来通过配置文件自动注入该对象
	i.col = ioc_mongo.DB().Collection(resource.AppName)
	i.log = logger.Sub(resource.AppName)
	i.validator = validator.New()
	return nil
}

// 把当前的controller也注册给grpc server, 对外提供grpc服务
func (i *impl) Registry(r *grpc.Server) {
	resource.RegisterRPCServer(r, i)
}

func (i *impl) Name() string {
	return resource.AppName
}
